package cn.xiaobai.captcha.handler.captchatype;

import cn.xiaobai.captcha.config.CaptchaProperties;
import cn.xiaobai.captcha.handler.AbstractTypeHandler;
import cn.xiaobai.captcha.utils.httpcontext.HttpContextUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 验证码类型处理器, 根据不同类型验证码进行生成
 *
 * @Author yangdaji
 * @Date 2024/4/18
 * @PackageName cn.xiaobai.captcha.handler
 * @ClassName CaptchaTypeHandler
 */
@Slf4j
public abstract class AbstractCaptchaAbstractTypeHandler extends AbstractTypeHandler {

    public AbstractCaptchaAbstractTypeHandler() {
    }

    public AbstractCaptchaAbstractTypeHandler(CaptchaProperties captchaProperties) {
        super(captchaProperties);
    }

    /**
     * 生成验证码
     *
     * @return 验证码
     */
    public abstract String createGeneral();


    /**
     * 设置统一响应流
     *
     * @return
     */
    protected ServletOutputStream getOutputStream() {
        HttpServletResponse response = HttpContextUtils.getResponse();
        response.setContentType("image/png");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setDateHeader("Expires", 0);

        try {
            return response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
